package com.news.controller;

import entity.Attribute;
import entity.Type;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.IUserService;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
     IUserService ius;


    //登录
    @RequestMapping("/login")
    public String m1(User user, HttpSession session) {
        System.out.println(user);
        User user1 = ius.userLogin(user);
        System.out.println(user1);
        if (user1 != null) {
            session.setAttribute("user", user1);
            System.out.println(session.getMaxInactiveInterval());
            return "redirect:/news/index";
        }
        return "redirect:/login.jsp";
    }

    // 注册
    @RequestMapping("/save")
    public String m2(User user, HttpServletResponse res,String email,HttpSession session) {
        try {
            Attribute attribute = new Attribute("email",email);
            List<Attribute> list = new ArrayList<>();
            list.add(attribute);
            user.setAttributes(list);
            user.setId(UUID.randomUUID().toString().replace("-",""));
            user.setType(Type.QQ);
            ius.registerUser(user);
            session.setAttribute("user", user);
            return "redirect:/news/index";
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    // 更新用户信息有就修改没有就添加
    @RequestMapping("/update")
    public String m3(String id, String email, HttpServletResponse res) throws IllegalAccessException, IOException {
        System.out.println(email);
        Attribute attribute = new Attribute("email",email);
        ius.updateAttribute(id, attribute);
       return "redirect:/user/queryId";
    }

    // 删除用户属性
    @RequestMapping("/deleteAttr")
    public void m4(User user, HttpServletResponse res) throws IOException, IllegalAccessException {
        List<Attribute> attributes = user.getAttributes();
        for (Attribute attr : attributes) {
            ius.deleteAttribute(user.getId(),attr.getColumn());
        }
        res.getWriter().print("ok");
        res.getWriter().close();
    }
    // 用户id删除
    @RequestMapping("/deleteid")
    public void m5(String id, HttpServletResponse res) throws IOException {
        System.out.println(id);
        ius.deleteUserById(id);
        res.getWriter().print("ok");
        res.getWriter().close();
    }
    // 按照attribute删除
    @RequestMapping("/deleteAttribute")
    public void m6(User user, HttpServletResponse res) throws IOException {
        List<Attribute> attr = user.getAttributes();
        Attribute[] objects = (Attribute[]) attr.toArray();
        ius.deleteUserByAttribute(objects);
        res.getWriter().print("ok");
        res.getWriter().close();
    }
    // 用户id查询接口
    @RequestMapping("/queryId")
    public String  m7( HttpServletResponse res,HttpSession session,Model model) throws IOException {
        Map<String,Object> map=new HashMap<String,Object>();
        User user1 = (User)session.getAttribute("user");
        User user = ius.queryUserById(user1.getId());
       /* map.put("id",user.getId());
        map.put("name",user.getName());
        map.put("type",user.getType());
        for (Attribute attr : user.getAttributes()) {
            map.put(attr.getColumn(),attr.getValue());
        }*/
        System.out.println(user);
        model.addAttribute("user",user);
        return "contact";
    }
    // 分页查询
    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> m8(Integer page,Integer rows, HttpServletResponse res) throws IOException {
        Map<String,Object> map1 =new HashMap<String,Object>();
        List<User> users = ius.queryByAttributes(1, 1);
        Map<String,Object> map=new HashMap<String,Object>();
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for (User user : users) {
            System.out.println(user);
            map.put("id",user.getId());
            map.put("name",user.getName());
            map.put("type",user.getType());
            if(user.getAttributes()!=null){
                for (Attribute attr : user.getAttributes()) {
                    map.put(attr.getColumn(),attr.getValue());
                }
            }
            System.out.println(map);
            list.add(map);
        }

        map1.put("total",ius.queryAll());
        map1.put("rows",list);
        System.out.println(map1);
        return map1;
    }
}
